<template>
  <div class="form-designer-left">
    <div class="component-panel">
      <div class="file-system">
        <div class="file-system-header">
          <span class="tag" @click="selectTag('resource')" :class="{ 'select-tag': tag === 'resource' }">页面</span>
          <span class="tag" @click="selectTag('component')" :class="{ 'select-tag': tag === 'component' }">大纲</span>
          <div class="edit-icon-group">
            <div class="edit-icon-group-item-1">
              <div class="edit-icon">
                <svgSearchLine />
              </div>
              <el-input style="width: 150px;" v-model="search" placeholder="搜索" />
            </div>
            <div class="edit-icon-group-item-2">
              <div class="edit-icon">
                <svgFileNewLine />
              </div>
              <div class="edit-icon">
                <svgNewFolderLine />
              </div>
            </div>
          </div>
        </div>
        <div class="file-system-content">
          <!-- 展开第一个节点 -->
          <el-tree :data="treeData" :props="defaultProps" :default-expanded-keys="[1]">
            <template #default="{ node, data }">
              <div class="tree-node">
                <div class="tree-icon">
                  <component viewBox="0 0 24 24" :is="getNodeIcon(data, node)" class="tree-icon" />
                </div>
                <span class="tree-label">{{ node.label }}</span>
              </div>
            </template>
          </el-tree>
        </div>
      </div>

      <div class="file-system">
        <div class="file-system-header">
          <span class="tag" @click="selectTag('resource')" :class="{ 'select-tag': tag === 'resource' }">资源库</span>
          <span class="tag" @click="selectTag('component')" :class="{ 'select-tag': tag === 'component' }">组件库</span>
          <div class="edit-icon-group">
            <div class="edit-icon-group-item-1">
              <div class="edit-icon">
                <svgSearchLine />
              </div>
              <el-input style="width: 150px;" v-model="search" placeholder="搜索" />
            </div>
            <div class="edit-icon-group-item-2">
              <div class="edit-icon">
                <svgMoreLine />
              </div>
            </div>
          </div>
        </div>
        <div class="grid-container">
          <div v-for="item in gridItems" :key="item.id" class="grid-item" draggable="true"
            @click="handleGridItemClick(item)" @dragstart="handleDragStart($event, item)" @dragend="handleDragEnd">
            <div class="grid-item-image">
              <component viewBox="0 0 24 24" :is="item.icon" class="grid-icon" />
            </div>
            <div class="grid-item-text">{{ item.name }}</div>
          </div>
        </div>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">
import svgTextboxLine from '@/assets/svg/textbox_line.svg'
import svgSearchLine from '@/assets/svg/search_line.svg'
import svgFileNewLine from '@/assets/svg/file_new_line.svg'
import svgNewFolderLine from '@/assets/svg/new_folder_line.svg'
import svgFolderLine from '@/assets/svg/folder_line.svg'
import svgFolderOpenLine from '@/assets/svg/folder_open_line.svg'
import svgDocumentLine from '@/assets/svg/document_line.svg'
import svgMoreLine from '@/assets/svg/more_line.svg'
import svgTextLine from '@/assets/svg/text_line.svg'
import svgCodeLine from '@/assets/svg/code_line.svg'
import svgBlingLine from '@/assets/svg/bling_line.svg'
import svgPaintLine from '@/assets/svg/paint_line.svg'
import svgFontLine from '@/assets/svg/font_line.svg'
import svgGridLine from '@/assets/svg/grid_line.svg'
import { ref } from 'vue'


const tag = ref('resource')
const search = ref('')

// 网格布局数据
const gridItems = ref([
  {
    id: 1,
    name: '文本输入',
    icon: svgTextboxLine,
    type: 'IFormInput'
  },
  {
    id: 10,
    name: '布局容器',
    icon: svgGridLine,
    type: 'IFormLayout'
  },
  {
    id: 2,
    name: '多行文本',
    icon: svgTextLine,
    type: 'textarea'
  },
  {
    id: 3,
    name: '下拉选择',
    icon: svgCodeLine,
    type: 'select'
  },
  {
    id: 4,
    name: '单选按钮',
    icon: svgBlingLine,
    type: 'radio'
  },
  {
    id: 5,
    name: '复选框',
    icon: svgCodeLine,
    type: 'checkbox'
  },
  {
    id: 6,
    name: '颜色选择',
    icon: svgPaintLine,
    type: 'color'
  },
  {
    id: 7,
    name: '字体设置',
    icon: svgFontLine,
    type: 'font'
  },
  {
    id: 8,
    name: '图片上传',
    icon: svgDocumentLine,
    type: 'image'
  },
  {
    id: 9,
    name: '文件上传',
    icon: svgFileNewLine,
    type: 'file'
  },
  {
    id: 10,
    name: '表格',
    icon: svgGridLine,
    type: 'table'
  }
])
const treeData = ref([
  {
    id: 1,
    label: '项目文件夹',
    type: 'folder',
    children: [
      {
        id: 2,
        label: '表单设计',
        type: 'folder',
        children: [
          {
            id: 3,
            label: '用户信息表单.json',
            type: 'file'
          },
          {
            id: 4,
            label: '订单表单.json',
            type: 'file'
          }
        ]
      },
      {
        id: 5,
        label: '组件库',
        type: 'folder',
        children: [
          {
            id: 6,
            label: '自定义组件.vue',
            type: 'file'
          }
        ]
      },
      {
        id: 7,
        label: 'README.md',
        type: 'file'
      }
    ]
  }
])
const defaultProps = ref({
  children: 'children',
  label: 'label'
})


// 组件定义（暂时未使用，保留以备将来使用）
/*
const basicComponents: ComponentConfig[] = [
  {
    type: 'input',
    name: '文本输入',
    description: '单行文本输入框',
    icon: svgTextboxLine,
    defaultConfig: {
      label: '文本输入',
      placeholder: '请输入内容',
      required: false
    }
  },
  {
    type: 'textarea',
    name: '多行文本',
    description: '多行文本输入框',
    icon: svgTextLine,
    defaultConfig: {
      label: '多行文本',
      placeholder: '请输入内容',
      required: false
    }
  },
  {
    type: 'select',
    name: '下拉选择',
    description: '下拉选择框',
    icon: svgCodeLine,
    defaultConfig: {
      label: '下拉选择',
      placeholder: '请选择',
      required: false,
      options: [
        { label: '选项1', value: 'option1' },
        { label: '选项2', value: 'option2' }
      ]
    }
  },
  {
    type: 'radio',
    name: '单选按钮',
    description: '单选按钮组',
    icon: svgBlingLine,
    defaultConfig: {
      label: '单选按钮',
      required: false,
      options: [
        { label: '选项1', value: 'option1' },
        { label: '选项2', value: 'option2' }
      ]
    }
  },
  {
    type: 'checkbox',
    name: '复选框',
    description: '复选框',
    icon: svgCodeLine,
    defaultConfig: {
      label: '复选框',
      required: false
    }
  }
]
*/


// 选择标签
const selectTag = (str: string) => {
  tag.value = str
}

// 获取节点图标
const getNodeIcon = (data: { type: string }, node: { expanded: boolean }) => {
  if (data.type === 'file') {
    return svgDocumentLine
  } else if (data.type === 'folder') {
    // 根据节点是否展开来决定显示哪个图标
    return node.expanded ? svgFolderOpenLine : svgFolderLine
  }
  return svgDocumentLine // 默认图标
}

// 处理网格项点击
const handleGridItemClick = (item: { id: number; name: string; icon: string; type: string }) => {
  console.log('点击了网格项:', item)
  // 这里可以添加具体的业务逻辑，比如添加到表单设计器
}

// 拖拽开始
const handleDragStart = (event: DragEvent, item: { id: number; name: string; icon: string; type: string }) => {
  if (event.dataTransfer) {
    // 设置拖拽数据
    event.dataTransfer.setData('application/json', JSON.stringify({
      type: 'form-field',
      componentType: item.type,
      componentName: item.name,
      icon: item.icon
    }))
    event.dataTransfer.effectAllowed = 'copy'
  }
}

// 拖拽结束
const handleDragEnd = (event: DragEvent) => {
  // 拖拽结束后的清理工作
  console.log('拖拽结束')
}

</script>

<style scoped lang="scss">
.form-designer-left {
  height: 100%;
}

.component-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.file-system {
  &:nth-child(1) {
    border-bottom: 1px solid var(--layout-boder-color);

    .file-system-content {
      height: 200px;
      overflow-y: scroll;

      // 隐藏Element Plus树形组件的默认图标
      :deep(.el-tree-node__expand-icon) {
        display: none !important;
      }
    }
  }

  &-header {
    padding: 10px 10px 0 10px;
    color: #A1A1AA;

    .edit-icon-group {
      display: flex;
      align-items: center;

      &-item-1 {
        flex: 1;
        display: flex;
        align-items: center;

      }

      &-item-2 {
        width: 100px;
        display: flex;
        justify-content: end;
      }
    }

    .edit-icon {
      padding: 4px;
      border-radius: 5px;
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      transform: scale(0.8);

      &:hover {
        background: rgba($color: #000000, $alpha: 0.1);
      }

      &.active {
        background: rgba($color: #409eff, $alpha: 0.2);
        color: #409eff;
      }
    }
  }

  &-content {
    padding: 0 20px;

    .tree-node {
      display: flex;
      align-items: center;

      .tree-icon {
        width: 16px;
        height: 16px;
        margin-right: 8px;
        flex-shrink: 0;
      }

      .tree-label {
        font-size: 12px;
        color: #606266;
      }
    }
  }
}


.tag {
  color: #A1A1AA;
  cursor: pointer;
  margin-right: 10px;
}

.select-tag {
  color: #3B82F6;
  font-weight: bold;
}


.panel-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.component-categories {
  .category {
    margin-bottom: 24px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .category-header {
    margin-bottom: 12px;

    h4 {
      margin: 0;
      font-size: 14px;
      font-weight: 600;
      color: #606266;
      padding: 8px 12px;
      background: #f0f2f5;
      border-radius: 6px;
    }
  }
}

.component-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.component-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  background: white;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    border-color: #409eff;
    background: #f0f9ff;
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.2);
  }

  &:active {
    transform: scale(0.98);
  }
}

.component-icon {
  width: 32px;
  height: 32px;
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  border-radius: 6px;
  flex-shrink: 0;
}

.component-info {
  flex: 1;
  min-width: 0;

  .component-name {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 4px;
  }

  .component-desc {
    font-size: 12px;
    color: #909399;
    line-height: 1.4;
  }
}

// 网格布局样式
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  padding: 8px;
}

.grid-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  min-height: 80px;

  &:hover {
    background: #f0f9ff;
  }

  &:active {
    transform: translateY(0) scale(0.98);
  }

  &[draggable="true"] {
    cursor: grab;

    &:active {
      cursor: grabbing;
    }
  }
}

.grid-item-image {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  margin-bottom: 8px;
  border-radius: 6px;
  flex-shrink: 0;
}

.grid-icon {
  width: 20px;
  height: 20px;
  color: #606266;
}

.grid-item-text {
  font-size: 12px;
  color: #303133;
  text-align: center;
  line-height: 1.4;
  word-break: break-all;
  max-width: 100%;
}
</style>
